package org.ducinaltum.android.prima;

import java.text.SimpleDateFormat;

import org.ducinaltum.android.prima.services.ServiceException;
import org.ducinaltum.android.prima.services.DucInAltumService.Event;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

public class EventDetailsFragment extends Fragment {

	public static final String EVENT_ID = "event_id";
	private static final String DATE_FORMAT = "MMMM yyyy\nEEEE d\nH:mm";

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
    		final Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_event_details, container, false);
    }

    @Override
    public void onActivityCreated(final Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        long eventId = getArguments().getLong(EVENT_ID);
        Event event = Globals.getEventRespositoryService().getEventFromCache(eventId);

		if (event != null) showEvent(event);
		else new LoadEventTask().execute(eventId);
    }

    private void showEvent(final Event event) {
    	SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
		CharSequence startDate = dateFormat.format(event.startDate);
		CharSequence endDate = dateFormat.format(event.endDate);
		String address = event.city + ", " + event.state;
		if (event.address != null) address = event.address + "\n" + address;
		String typeAge = event.type + " - " + event.age;
    	View view = getView();

    	if (view != null) {
			((ProgressBar)view.findViewById(R.id.pbEventLoading)).setVisibility(View.INVISIBLE);
			((TextView)view.findViewById(R.id.tvEventError)).setVisibility(View.INVISIBLE);
			((TextView)view.findViewById(R.id.tvEventStartDate)).setText(startDate);
			((TextView)view.findViewById(R.id.tvEventEndDate)).setText(endDate);
			((TextView)view.findViewById(R.id.tvEventAddress)).setText(address);
			((TextView)view.findViewById(R.id.tvEventTitle)).setText(event.title);
			((TextView)view.findViewById(R.id.tvEventTypeAge)).setText(typeAge);
			((TextView)view.findViewById(R.id.tvEventDescription)).setText(event.description);
			((TextView)view.findViewById(R.id.tvEventContact)).setText(event.contact);
    	}
    }

    private void showError() {
    	View view = getView();
		((ProgressBar)view.findViewById(R.id.pbEventLoading)).setVisibility(View.INVISIBLE);
		((TextView)view.findViewById(R.id.tvEventError)).setVisibility(View.VISIBLE);
    }


    private class LoadEventTask extends AsyncTask<Long, Void, Event> {

    	protected Event doInBackground(final Long... eventId) {
    		Event event = null;
			try { event = Globals.getEventRespositoryService().getEvent(eventId[0]); }
			catch (ServiceException ex) {}
			return event;
        }

        protected void onPostExecute(final Event event) {
			if (event != null) showEvent(event);
			else showError();
        }
    }
}
